﻿using LocoyApp.Locoy.Entry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LocoyApp.Locoy
{
    public class ImagesPicker : LocoyCode
    {
        /// <summary>
        /// 执行方法，不能修改类和方法名称。
        /// </summary>
        /// <param name="content">标签内容</param>
        /// <param name="response">页面响应，包含了Url、原始Html等属性</param>
        /// <returns>返回处理后的标签内容</returns>
        public string Run(string content, ResponseEntry response)
        {
            if (string.IsNullOrEmpty(content) || string.IsNullOrWhiteSpace(content) || content.IndexOf("src=") == -1)
            {
                return "";
            }

            var regex = new System.Text.RegularExpressions.Regex(@"src=""(.+?)""");
            var matches = regex.Matches(content);
            var images = new List<string>();
            foreach(System.Text.RegularExpressions.Match match in matches) 
            {
                images.Add(match.Groups[1].Value);
            }

            return string.Join("|", images);
        }
    }
}
